package com.sanjiahantang.youjiaclient.module.me.require.presenters;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.AbsListView;

import com.sanjiahantang.uilibrary.adapter.listview.BaseAdapterHelper;
import com.sanjiahantang.uilibrary.adapter.listview.QuickAdapter;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiaclient.R;
import com.sanjiahantang.youjiaclient.base.BaseMvpActivity;
import com.sanjiahantang.youjiaclient.base.BaseMvpPresenter;
import com.sanjiahantang.youjiaclient.base.BaseView;
import com.sanjiahantang.youjiaclient.base.CallBack;
import com.sanjiahantang.youjiaclient.config.ConfigValue;
import com.sanjiahantang.youjiaclient.config.SPConfig;
import com.sanjiahantang.youjiaclient.module.common.beans.StringBean;
import com.sanjiahantang.youjiaclient.module.me.contract.models.IContractModel;
import com.sanjiahantang.youjiaclient.module.me.contract.models.impl.ContractModel;
import com.sanjiahantang.youjiaclient.module.me.order.beans.CreateServiceOrderResponseBean;
import com.sanjiahantang.youjiaclient.module.me.order.beans.RequireOrderBean;
import com.sanjiahantang.youjiaclient.module.me.order.models.IOrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.models.impl.OrderModel;
import com.sanjiahantang.youjiaclient.module.me.order.views.impl.OrderConfirmActivity;
import com.sanjiahantang.youjiaclient.module.me.require.beans.BidDesignerBean;
import com.sanjiahantang.youjiaclient.module.me.require.beans.RequireListBean;
import com.sanjiahantang.youjiaclient.module.me.require.models.IRequireModel;
import com.sanjiahantang.youjiaclient.module.me.require.models.impl.RequireModel;
import com.sanjiahantang.youjiaclient.module.me.require.views.RequireView;
import com.sanjiahantang.youjiaclient.module.me.require.views.impl.RequireDetailActivity;
import com.sanjiahantang.youjiaclient.utils.SPUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import static com.sanjiahantang.youjiaclient.config.StaticValue.DEFAULT_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PAGE_SIZE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PARAM;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_PHONE;
import static com.sanjiahantang.youjiaclient.config.StaticValue.KEY_TOKEN;
import static com.sanjiahantang.youjiaclient.config.StaticValue.RESPONSE_CODE_SUC;

/**
 * Created by zy on 2017/10/24.
 */

public class RequirePresenter extends BaseMvpPresenter<RequireView> {

    private IRequireModel requireModel;
    private QuickAdapter<RequireListBean.Item> listAdapter;
    private IOrderModel orderModel;
    private IContractModel contractModel;

    private HashMap<Integer, String> stateMap, bidStateMap;

    public RequirePresenter() {
        requireModel = new RequireModel();
        orderModel = new OrderModel();
        contractModel = new ContractModel();
        stateMap = new HashMap<>();
        bidStateMap = new HashMap<>();
        stateMap.put(0, "待接单");
        stateMap.put(1, "已接单");
        stateMap.put(2, "接单完成");
        stateMap.put(3, "待接关闭");

        bidStateMap.put(0, "待投标");
        bidStateMap.put(1, "已投标");
        bidStateMap.put(2, "投标结束");
    }

    public void bindList(Context context, AbsListView listView) {
        if (listAdapter == null) {
            listAdapter = new QuickAdapter<RequireListBean.Item>(context, R.layout.item_require_list) {
                @Override
                protected void convert(BaseAdapterHelper helper, final RequireListBean.Item item) {
                    if (item.getIsBidProject() == 1 && item.getDesignStateId() == 0) {
                        helper.setText(R.id.txtState, bidStateMap.get(item.getIsBidProjectState()));
                        helper.getView().setOnClickListener(null);
                        if (item.getIsBidProjectState() == 1) {
                            helper.setVisible(R.id.btnAction, true);
                            helper.getView(R.id.btnAction).setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    Intent intent = new Intent(context, RequireDetailActivity.class);
                                    intent.putExtra(KEY_ID, item.getId());
                                    context.startActivity(intent);
                                }
                            });
                        } else {
                            helper.setVisible(R.id.btnAction, false);
                        }

                    } else {
                        helper.setVisible(R.id.btnAction, false);
                        helper.setText(R.id.txtState, stateMap.get(item.getDesignStateId()));
                        helper.getView().setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                Intent intent = new Intent(context, RequireDetailActivity.class);
                                intent.putExtra(KEY_ID, item.getId());
                                context.startActivity(intent);
                            }
                        });
                    }
                    helper.setText(R.id.txtRequireName, item.getApartmentName());
                    helper.setText(R.id.txtCity, item.getAddress());
                    helper.setText(R.id.txtDesignerName, item.getUserName());
                    helper.setText(R.id.txtTime, item.getFinishDate().replace('T',' ').substring(0, "yyyy-MM-dd HH:mm".length()));


                }
            };
        }
        listView.setAdapter(listAdapter);
    }

    public void loadRequireList(Context context) {
        Map<String, Object> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put(KEY_PAGE_SIZE, DEFAULT_PAGE_SIZE);
        param.put(KEY_PAGE, 0);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put(KEY_PHONE, SPUtils.get(context, SPConfig.PHONE, "").toString());
        param.put(KEY_PARAM, map);
        mView.showProgress();
        requireModel.loadRequireList(context, param, new CallBack<RequireListBean>() {
            @Override
            public void onSuccess(RequireListBean response, Context context, Object tag) {
                hideProgress();
                if (response.getCode() == RESPONSE_CODE_SUC) {
                    listAdapter.clear();
                    if (response.getData() != null) {
                        listAdapter.addAll(response.getData().getList());
                    }
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
            }
        });
    }

    private void hideProgress() {
        if (mView != null) {
            mView.hideProgress();
        }
    }

    public void PayOrderDetail(Context context, CreateServiceOrderResponseBean.Data data) {
        mView.showProgress();
        requireModel.PayMyOrderRequire(context, data, new PayMyOrderRequireDetailCallBack());
    }

    public void findRequireBidInfoForCustomer(Context context, int requireId, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("ucriId", String.valueOf(requireId));
        if (mView == null) return;
        mView.showProgress();
        requireModel.findRequireBidInfoForCustomer(context, param, new CallBack<BidDesignerBean>() {
            @Override
            public void onSuccess(BidDesignerBean response, Context context, Object tag) {
                if (mView == null) return;
                mView.hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                } else {
                    listener.onLoad(new ArrayList<BidDesignerBean.Data>());
                }

            }

            @Override
            public void onFailure(String message) {
                if (mView == null) return;
                mView.hideProgress();
                listener.onLoad(new ArrayList<BidDesignerBean.Data>());
            }
        });
    }

    public class PayMyOrderRequireDetailCallBack implements CallBack<RequireListBean> {
        @Override
        public void onSuccess(RequireListBean response, Context context, Object tag) {

            Utils.showToast(context, "" + response.getCode());
        }

        @Override
        public void onFailure(String message) {

            message.toString();

        }
    }


    public void findUserRequireByIdPhone(Context context, final int requireId, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId", String.valueOf(requireId));
        contractModel.findUserRequireByIdPhone(context, param, new CallBack<RequireListBean>() {
            @Override
            public void onSuccess(final RequireListBean requireListBean, Context context, Object tag) {
                if (requireListBean != null && requireListBean.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(requireListBean.getItem());
                } else {
                    listener.onLoad(null);
                }
            }

            @Override
            public void onFailure(String message) {
                listener.onLoad(null);
            }
        });
    }

    /**
     * /project/phone/plan/searchPriceDesignPhoneForRole
     *
     * @param context
     * @param id
     */
    public void getRequireOrderPrice(final Context context, int id, final BaseView.OnLoadListener listener) {
        Map<String, String> param = new HashMap<>();
        param.put(KEY_TOKEN, ConfigValue.DATA_KEY);
        param.put("requireId", String.valueOf(id));
        param.put("roleType","0");
        mView.showProgress();
        orderModel.searchPriceDesignPhoneForRole(context, param, new CallBack<RequireOrderBean>() {
            @Override
            public void onSuccess(RequireOrderBean response, Context context, Object tag) {
                if (mView == null) return;
                hideProgress();
                if (response != null && response.getCode() == RESPONSE_CODE_SUC) {
                    listener.onLoad(response.getData());
                } else {
                    Utils.showToast(context, "很抱歉，需求单不见了~");
                    ((Activity) context).finish();
                }
            }

            @Override
            public void onFailure(String message) {
                hideProgress();
                Utils.showToast(context, "很抱歉，需求单不见了~");
                ((Activity) context).finish();
            }
        });
    }
}
